# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=clang

pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-clang-analyzer" "${MINGW_PACKAGE_PREFIX}-llvm") # "${MINGW_PACKAGE_PREFIX}-compiler-rt"

pkgver=3.4.2
_base_ver=3.4
pkgrel=1
pkgdesc="LC language family frontend for LLVM (mingw-w64)"
arch=('any')
url="http://llvm.org"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "python2" "tar")
depends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=(http://llvm.org/releases/$pkgver/llvm-$pkgver.src.tar.gz{,.sig}
        http://llvm.org/releases/$pkgver/cfe-$pkgver.src.tar.gz{,.sig}
        http://llvm.org/releases/$_base_ver/compiler-rt-$_base_ver.src.tar.gz{,.sig}
        http://llvm.org/releases/$_base_ver/test-suite-$_base_ver.src.tar.gz{,.sig}
        http://llvm.org/releases/$_base_ver/libcxx-$_base_ver.src.tar.gz{,.sig}
        llvm-3.3-tests-ulimit-mingw32-fix.patch
        llvm-3.3-tests-hack-remove-ocamlopt_executable.patch
        clang-3.4-add-recent-gcc-versions.patch
        clang-3.4-enable-tls.patch)
sha256sums=('17038d47069ad0700c063caed76f0c7259628b0e79651ce2b540d506f2f1efd7'
            'SKIP'
            '5ba6f5772f8d00f445209356a7daf83c5bca2da5acd10de517ad2359ae95bc10'
            'SKIP'
            'f37c89b1383ce462d47537a0245ac798600887a9be9f63073e16b79ed536ab5c'
            'SKIP'
            '0ff3bbb8514dd7d14b747300994fc8898c8d17e1cf071fcc25d647efef716140'
            'SKIP'
            'bd9f200279f0eb19fc28ed2d9d607dfd38e2e7d92bf52e7fb2161f89212e0deb'
            'SKIP'
            'e444e4390ceb6280f4e610b2ef39aed68c032adaea5c545dd850c0956ed29bdd'
            '03b87bde0a630ec89f848b05aa4008c3d59616eccfaaad7a87e1980e4dfb98ce'
            '88d8bb94e990e6cbff9fecc728aebdf0812ac6e58575701d77854bccfca1134c'
            '70b246307b98a3cb5cccbb39b6b8e22ac5cb28cbf81504176df0992400aeed70')

noextract=(cfe-$pkgver.src.tar.gz)

prepare() {
  plain "Extracting clang-$pkgver.src.tar.gz due to symlink(s) without pre-existing target(s)"
  [ -d $srcdir/cfe-$pkgver ] && rm -rf $srcdir/cfe-$pkgver
  [ -d $srcdir/cfe-$pkgver ] || tar -xzvf $startdir/cfe-$pkgver.src.tar.gz -C $srcdir || true

  # since 3.4.2 .src is appended to the directory?
  [ -d $srcdir/clang-$pkgver ] && rm -rf $srcdir/clang-$pkgver
  mv "$srcdir/llvm-$pkgver.src" "$srcdir/llvm-$pkgver"
  mv "$srcdir/cfe-$pkgver.src" "$srcdir/clang-$pkgver"

  # Prevent various ulimit failures on MSYS2.
  cd "$srcdir/llvm-$pkgver"
  patch -p1 -i ${srcdir}/llvm-3.3-tests-ulimit-mingw32-fix.patch
  patch -p1 -i ${srcdir}/llvm-3.3-tests-hack-remove-ocamlopt_executable.patch

  cd "$srcdir/clang-$pkgver"
  patch -p1 -i ${srcdir}/clang-3.4-add-recent-gcc-versions.patch
  patch -p1 -i ${srcdir}/clang-3.4-enable-tls.patch

  cd "$srcdir/llvm-$pkgver"

  # At the present, clang must reside inside the LLVM source code tree to build
  # See http://llvm.org/bugs/show_bug.cgi?id=4840
  [ -d tools/clang ] && rm -rf tools/clang
  mv "$srcdir/clang-$pkgver" tools/clang
  [ -d projects/compiler-rt ] && rm -rf projects/compiler-rt
  mv "$srcdir/compiler-rt-$_base_ver" projects/compiler-rt
  # [ -d projects/test-suite ] && rm -rf projects/test-suite
  # mv "$srcdir/test-suite-$_base_ver" projects/test-suite
  # [ -d projects/libcxx-$_base_ver ] && rm -rf projects/libcxx
  # mv "$srcdir/libcxx-$_base_ver" projects/libcxx

  # Fix docs installation directory
  sed -i 's:\$(PROJ_prefix)/docs/llvm:$(PROJ_prefix)/share/doc/llvm:' \
    Makefile.config.in

  # Make -flto work; use ld.gold instead of the default linker
  # patch -d tools/clang -Np1 -i "$srcdir/clang-3.3-use-gold-linker.patch"

}

build() {
  cd "$srcdir"
  mkdir -p build-${MINGW_CHOST}
  cd build-${MINGW_CHOST}
  echo 'ac_cv_have_decl_strerror_s=${ac_cv_have_decl_strerror_s=no}' > config.cache

  # Include location of libffi headers in CPPFLAGS
  CPPFLAGS+=" $(pkg-config --cflags libffi)"

  ../llvm-$pkgver/configure \
    -C \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-sysroot=${MINGW_PREFIX} \
    --enable-targets=x86,x86_64,arm,mips \
    --enable-optimized \
    --enable-assertions \
    --disable-pthreads \
    --with-python=/usr/bin/python2 \
    --enable-libffi \
    --enable-docs
    #--enable-shared
    #--enable-embed-stdcxx
    #--enable-libcpp
    #--enable-cxx11
    #
    #--enable-ltdl-install
    #
    #--with-c-include-dir
    #--with-gcc-toolchain
    #--with-default-sysroot
    #--with-binutils-include
    #--with-bug-report-url
  make
  setup
}

check() {
  plain "skipping make check..."
  #cd "$srcdir/build-${MINGW_CHOST}"
  #make check
}

setup() {
  local destdir=${srcdir}/dest
  cd "$srcdir/build-${MINGW_CHOST}/tools/clang"
  make -j1 DESTDIR="$destdir/clang" install
  # Fix mis-named clang doc folder (is llvm, should be clang)
  mv ${destdir}/clang${MINGW_PREFIX}/share/doc/llvm ${destdir}/clang${MINGW_PREFIX}/share/doc/clang

  install -d ${destdir}/clang-analyzer${MINGW_PREFIX}/{bin,lib/clang-analyzer}
  for _tool in scan-{build,view}; do
    cp -r $srcdir/llvm-$pkgver/tools/clang/tools/$_tool ${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer/
    ln -s ${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer/$_tool/$_tool ${destdir}/clang-analyzer${MINGW_PREFIX}/bin/
  done

  # scan-build looks for clang within the same directory
  install -d ${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer/scan-build/
  ln -s ${destdir}/clang${MINGW_PREFIX}/bin/clang.exe ${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer/scan-build/

  # Relocate man page
  install -d ${destdir}/clang-analyzer${MINGW_PREFIX}/share/man/man1
  mv ${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer/scan-build/scan-build.1 \
    ${destdir}/clang-analyzer${MINGW_PREFIX}/share/man/man1/

  # Install CMake stuff
  install -d "${destdir}"/llvm${MINGW_PREFIX}/share/llvm/cmake/{modules,platforms}
  install -Dm644 $srcdir/llvm-$pkgver/cmake/modules/*.cmake "${destdir}"/llvm${MINGW_PREFIX}/share/llvm/cmake/modules/
  install -Dm644 $srcdir/llvm-$pkgver/cmake/platforms/*.cmake "${destdir}"/llvm${MINGW_PREFIX}/share/llvm/cmake/platforms/

  # Use Python 2
  #sed -i \
  #  -e 's|env python$|&2|' \
  #  -e 's|/usr/bin/python$|&2|' \
  #  "${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer/scan-view/scan-view" \
  #  "${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer/scan-build/set-xcode-analyzer"

  # Compile Python scripts
  python2 -m compileall "${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer"
  python2 -O -m compileall "${destdir}/clang-analyzer${MINGW_PREFIX}/lib/clang-analyzer"
  install -Dm644 $srcdir/llvm-$pkgver/tools/clang/LICENSE.TXT "${destdir}/clang-analyzer${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE"

  # Move clang out of the way *before* installing llvm, both src and build. We'll put build back afterwards to allow re-packaging.
  # but the src move is undoing what prepare() did.
  mv "$srcdir/build-${MINGW_CHOST}/tools/clang" "$srcdir/clang-$MINGW_CHOST"
  mv "$srcdir/llvm-$pkgver/tools/clang"          "$srcdir/clang-$pkgver"       # Put src back where it was (clang)
  cd "$srcdir/build-${MINGW_CHOST}"
  make -j1 DESTDIR="$destdir/llvm" install
  mv "$srcdir/clang-$MINGW_CHOST" "$srcdir/build-${MINGW_CHOST}/tools/clang"

  # Undo more prepare() moves.
  #mv "$srcdir/llvm-$pkgver/projects/compiler-rt" "$srcdir/compiler-rt-$_base_ver" # Put src back where it was (the rest)
  #mv "$srcdir/llvm-$pkgver/projects/test-suite"  "$srcdir/test-suite-$_base_ver"
  #mv "$srcdir/llvm-$pkgver/projects/libcxx"      "$srcdir/libcxx-$_base_ver"
}

package_clang(){
  pkgdesc="C language family frontend for LLVM"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}")

  mv $srcdir/dest/clang/* $pkgdir
}

# package_compiler-rt(){
#   cd ${srcdir}/compiler-rt
# }

package_llvm(){
  pkgdesc="Low Level Virtual Machine"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc") # "${MINGW_PACKAGE_PREFIX}-compiler-rt"

  mv $srcdir/dest/llvm/* $pkgdir
}

package_clang-analyzer() {
  pkgdesc="A source code analysis framework"
  url="http://clang-analyzer.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}" "${MINGW_PACKAGE_PREFIX}-python2")

  mv $srcdir/dest/clang-analyzer/* $pkgdir
}

package_mingw-w64-i686-clang(){
  package_clang
}

package_mingw-w64-i686-clang-analyzer(){
  package_clang-analyzer
}

# package_mingw-w64-i686-compiler-rt(){
#   package_compiler-rt
# }

package_mingw-w64-i686-llvm(){
  package_llvm
}

package_mingw-w64-x86_64-clang(){
  package_clang
}

package_mingw-w64-x86_64-clang-analyzer(){
  package_clang-analyzer
}

# package_mingw-w64-x86_64-compiler-rt(){
#   package_compiler-rt
# }

package_mingw-w64-x86_64-llvm(){
  package_llvm
}
